#include "filemenu.h"
#include <QAction>
#include <QDebug>
#include "configmanager.h"
#include <QSettings>
#include "opendialog.h"
FileMenu::FileMenu(QWidget *parent)
    : QMenu(parent){
    setTitle("File"); // 设置菜单标题
    menuInit();       // 调用菜单初始化
   // 添加布局到菜单
    layout = new QVBoxLayout(this);
}

FileMenu::~FileMenu() {
    menuDestroy(); // 调用菜单销毁
}

void FileMenu::menuInit() {
    // 创建各个菜单项
    m_openProject = new QAction(tr("Open Project"), this);
    m_newProject = new QAction(tr("New Project"), this);
    m_saveProject = new QAction(tr("Save Project"), this);
    m_saveAsProject = new QAction(tr("Save As Project"), this);
    m_exit = new QAction(tr("Exit"), this);

    // 添加菜单项到菜单中
    addAction(m_openProject);
    addAction(m_newProject);
    addAction(m_saveProject);
    addAction(m_saveAsProject);
    addSeparator(); // 添加分隔符
    addAction(m_exit);

    // 连接菜单项信号到槽函数
    connect(m_openProject, &QAction::triggered, this, &FileMenu::slotOpenProject);
    connect(m_newProject, &QAction::triggered, this, &FileMenu::slotNewProject);
    connect(m_saveProject, &QAction::triggered, this, &FileMenu::slotSaveProject);
    connect(m_saveAsProject, &QAction::triggered, this, &FileMenu::slotSaveAsProject);
    connect(m_exit, &QAction::triggered, this, &FileMenu::signalExit);
}

void FileMenu::menuDestroy() {
    // 销毁各个菜单项
    delete m_openProject;
    delete m_newProject;
    delete m_saveProject;
    delete m_saveAsProject;
    delete m_exit;
}

void FileMenu::slotOpenProject(){
    qDebug() << "Open Project triggered";

        // 打开文件对话框，选择配置文件
        QString fileName = QFileDialog::getOpenFileName(this, tr("Open Config File"), "", tr("Config Files (*.ini);;All Files (*)"));
        if (fileName.isEmpty()) {
            return; // 如果用户取消选择，不进行操作
        }

        // 加载配置文件到 ConfigManager
        ConfigManager::getInstance()->setFile(fileName);
        SerialConfig config;
        try {
            config = ConfigManager::getInstance()->load();  // 返回配置结构体
        } catch (const ConfigManagerException &e) {
            QMessageBox::critical(this, tr("Error"), e.what());
            return;
        }
        // 创建并显示对话框
        OpenDialog *dialog = new OpenDialog(this);
        dialog->setValues(config);  // 使用配置数据填充对话框
        dialog->exec();             // 显示对话框

}

void FileMenu::slotNewProject() {
        QString fileName = QFileDialog::getSaveFileName(this, tr("New Config File"), "", tr("Config Files (*.ini);;All Files (*)"));
        if (fileName.isEmpty()) {
            return; // 用户取消操作
        }

        // 创建一个新的配置文件
        ConfigManager::getInstance()->setFile(fileName);
        // 使用 QSettings 写入默认配置（确保文件被创建）
        QSettings settings(fileName, QSettings::IniFormat);
        settings.beginGroup("Serial");
        settings.setValue("PortName", "");
        settings.setValue("BaudRate", "");
        settings.setValue("DataBits", "");
        settings.setValue("Parity", "");
        settings.setValue("StopBits", "");
        settings.setValue("FlowControl", "");
        settings.endGroup();

        QMessageBox::information(this, tr("New Config"), tr("New configuration file created successfully!"));
}

void FileMenu::slotSaveProject() {
        try {
            if (ConfigManager::getInstance()->save()) {
                QMessageBox::information(this, tr("Success"), tr("Configuration saved successfully!"));
            } else {
                QMessageBox::critical(this, tr("Error"), tr("Failed to save configuration."));
            }
        } catch (const ConfigManagerException &e) {
            QMessageBox::critical(this, tr("Error"), e.what());
        }
}

void FileMenu::slotSaveAsProject() {
       QString fileName = QFileDialog::getSaveFileName(this, tr("Save Config As"), "", tr("Config Files (*.ini);;All Files (*)"));
       if (fileName.isEmpty()) {
           return; // 用户取消操作
       }
       try {
           if (ConfigManager::getInstance()->saveAs(fileName)) {
               QMessageBox::information(this, tr("Success"), tr("Configuration saved as successfully!"));
           } else {
               QMessageBox::critical(this, tr("Error"), tr("Failed to save configuration."));
           }
       } catch (const ConfigManagerException &e) {
           QMessageBox::critical(this, tr("Error"), e.what());
       }
}
